package SwordForOffer.Problem8;

public class MyMin {
    public Integer minInReversingList(int[] array) {

        if (array == null){
            return null;
        }

        int liftIndex = 0;
        int rightIndex = array.length - 1;
        int midIndex = -1;

        while(liftIndex < rightIndex - 1){

            if (array[liftIndex] > array[rightIndex]){
                midIndex = (liftIndex + rightIndex)/2;
                if (array[midIndex] >= array[liftIndex]){
                    liftIndex = midIndex;
                }else {
                    rightIndex = midIndex;
                }


            }else if (array[liftIndex] == array[rightIndex]){
                midIndex = (liftIndex + rightIndex)/2;
                if (array[midIndex] < array[liftIndex]){
                    rightIndex = midIndex;
                }else if (array[midIndex] > array[rightIndex]){
                    liftIndex = midIndex;
                }else {
                    liftIndex++;
                    rightIndex--;
                }
            }

        }

        return array[rightIndex];
    }
}
